# 定义项目名称和可执行文件名称
PROJECT_NAME := vigilix
OUTPUT_DIR := ./bin
SERVER_BIN := $(PROJECT_NAME)-server
AGENT_BIN := $(PROJECT_NAME)-agent
CLIENT_BIN := $(PROJECT_NAME)-client
PUSH_BIN := $(PROJECT_NAME)-push

# 根据操作系统设置可执行文件后缀
ifeq ($(OS),Windows_NT)
    BIN_SUFFIX := .exe
else
    BIN_SUFFIX :=
endif
SERVER := server
PUSH := push
AGENT := agent
CLIENT := client

SERVER_CONFIG := $(SERVER)/config/config.yaml
PUSH_CONFIG := $(PUSH)/config/config.yaml
AGENT_CONFIG := $(AGENT)/config/config.yaml
CLIENT_CONFIG := $(CLIENT)/config/config.yaml

# 完整的可执行文件路径（输出到bin目录）
SERVER_BIN_FULL := $(OUTPUT_DIR)/$(SERVER_BIN)$(BIN_SUFFIX)
AGENT_BIN_FULL := $(OUTPUT_DIR)/$(AGENT_BIN)$(BIN_SUFFIX)
CLIENT_BIN_FULL := $(OUTPUT_DIR)/$(CLIENT_BIN)$(BIN_SUFFIX)
PUSH_BIN_FULL := $(OUTPUT_DIR)/$(PUSH_BIN)$(BIN_SUFFIX)

# 默认目标：编译所有组件
all: create-dir server agent client push
	@echo "All components built successfully! Output: $(OUTPUT_DIR)/"

# 创建输出目录
create-dir:
	@mkdir -p $(OUTPUT_DIR)

# 编译服务端
server:
	@echo "Building server..."
	cd server && go mod tidy && go build -o ../$(SERVER_BIN_FULL) main.go

# 编译代理端
agent:
	@echo "Building agent..."
	cd agent && go mod tidy && go build -o ../$(AGENT_BIN_FULL) main.go

# 编译客户端
client:
	@echo "Building client..."
	cd client && go mod tidy && go build -o ../$(CLIENT_BIN_FULL) main.go

# 编译推送端
push:
	@echo "Building push..."
	cd push && go mod tidy && go build -o ../$(PUSH_BIN_FULL) main.go

# 运行服务端
run-server: server
	@echo "Starting server..."
	./$(SERVER_BIN_FULL) -f $(SERVER_CONFIG)

# 运行代理端
run-agent: agent
	@echo "Starting agent..."
	./$(AGENT_BIN_FULL) -f $(AGENT_CONFIG)

# 运行客户端
run-client: client
	@echo "Starting client..."
	./$(CLIENT_BIN_FULL)

# 运行推送端
run-push: push
	@echo "Starting push..."
	./$(PUSH_BIN_FULL) -f $(PUSH_CONFIG)

# 清理编译产物
clean:
	@echo "Cleaning up..."
	rm -rf $(OUTPUT_DIR) logs/*

# 帮助信息
help:
	@echo "Available targets:"
	@echo "  all          - Build all components (server, agent, client, push)"
	@echo "  server       - Build server only"
	@echo "  agent        - Build agent only"
	@echo "  client       - Build client only"
	@echo "  push         - Build push only"
	@echo "  run-server   - Build and run server"
	@echo "  run-agent    - Build and run agent"
	@echo "  run-client   - Build and run client"
	@echo "  run-push     - Build and run push"
	@echo "  clean        - Clean all built binaries"
	@echo "  help         - Show this help message"

# 声明伪目标
.PHONY: all create-dir server agent client push run-server run-agent run-client run-push clean help